package com.cupk.inspection.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class AlarmService {

    @Autowired
    private JavaMailSender mailSender;

    public void sendAlertEmail(String toEmail, String deviceName, String location, String description) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            helper.setTo(toEmail);
            helper.setSubject("设备异常报警");
            helper.setText(
                "<h3>设备异常报警</h3>" +
                "<p>设备名称: " + deviceName + "</p>" +
                "<p>位置: " + location + "</p>" +
                "<p>异常描述: " + description + "</p>",
                true // 表示内容为 HTML
            );

            mailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}